package com.company.project.web.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.company.project.common.pojo.Rest;
import com.company.project.web.system.mapper.TechnicianServiceitemMapper;
import com.company.project.web.system.pojo.technician_serviceitem.TechnicianServiceitem;
import com.company.project.web.system.pojo.user.User;
import com.company.project.web.system.mapper.UserMapper;
import com.company.project.web.system.pojo.user.UserFrom;
import com.company.project.web.system.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 任建波
 * @since 2019-09-10
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    TechnicianServiceitemMapper technicianServiceitemMapper;

    /**
     * 修改用户
     */
    @Override
    @Transactional    public Rest updateUser(UserFrom userFrom) {
        User user = new User();
        BeanUtils.copyProperties(userFrom, user);
        // 修改用户
        userMapper.updateById(user);

        //  先删除技师会的服务项目
        UpdateWrapper deleteWrapper = new UpdateWrapper();
        deleteWrapper.eq("technician_id", userFrom.getId());
        technicianServiceitemMapper.delete(deleteWrapper);
        //  再添加技师会的服务项目
        Integer[] serviceitemIds = userFrom.getServiceitemIds();
        for (Integer serviceitemId: serviceitemIds){
            TechnicianServiceitem technicianServiceitem = new TechnicianServiceitem();
            technicianServiceitem.setTechnicianId(userFrom.getId());
            technicianServiceitem.setServiceitemId(serviceitemId);
            technicianServiceitemMapper.insert(technicianServiceitem);
        }

        return null;
    }
}
